<template>
  <d2-container>
    <el-form :model="FormSearch" status-icon label-width="100px" class="demo-ruleForm">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="条件查找" prop="name">
            <el-input
              type="text"
              v-model="FormSearch.name"
              placeholder="请输入物品种类"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="searchClick()">查询</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
    <!-- 数据表格 -->
    <d2-crud
      ref="d2Crud"
      edit-title="编辑种类"
      add-title="添加种类"
      :columns="columns"
      :data="data"
      :rowHandle="rowHandle"
      :edit-template="editTemplate"
      :add-template="addTemplate"
      :form-options="formOptions"
      :pagination="page"
      @row-edit="handleRowEdit"
      @row-add="handleRowAdd"
      @custom-emit-1="deleteGood"
      @dialog-cancel="handleDialogCancel"
      @pagination-current-change="paginationCurrentChange"
    >
      <el-button slot="header" style="margin-bottom: 5px;" @click="addRow">添加种类</el-button>
    </d2-crud>
  </d2-container>
</template>

<script>
import {
  selectGoodType,
  addGoodType,
  updateGoodType,
  deleteGoodType
} from "@/api/accountAdmin/index";

export default {
  data() {
    return {
      // 搜索数据
      FormSearch: {
        name: ""
      },
      // 字段名称
      columns: [
        {
          title: "种类名称",
          key: "name",
          align: "center"
        }
      ],
      // 列表数据
      data: [],
      // 操作列数据
      rowHandle: {
        align: "center",
        edit: {
          icon: "el-icon-edit",
          text: "编辑",
          size: "small"
        },
        custom: [
          {
            text: "删除",
            type: "primary",
            size: "mini",
            emit: "custom-emit-1"
          }
        ]
      },
      editTemplate: {
        name: {
          title: "种类名称",
          value: ""
        }
      },
      addTemplate: {
        name: {
          title: "种类名称",
          value: ""
        }
      },
      formOptions: {
        labelWidth: "80px",
        labelPosition: "left",
        saveLoading: false
      },
      // 分页数据
      page: {
        total: 0,
        pageIndex: 1,
        pageSize: 10
      }
    };
  },
  created() {
    let params = {
      page: this.page.pageIndex,
      limit: this.page.pageSize
    };
    this.init(params);
  },
  methods: {
    // 初始化身体数据列表
    init(params) {
      selectGoodType(params)
        .then(res => {
          console.log(res);
          if (res.success) {
            this.data = res.rows;
            this.page.total = res.total;
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 分页数改变，调用
    paginationCurrentChange(pageIndex) {
      let params = {
        limit: this.page.pageSize,
        name: this.FormSearch.name
      };
      params.page = this.page.pageIndex = pageIndex;
      this.init(params);
    },

    // 普通的新增
    addRow() {
      this.$refs.d2Crud.showDialog({
        mode: "add"
      });
    },

    // 新增成功
    handleRowAdd(row, done) {
      this.formOptions.saveLoading = true;
      let params = row;
      addGoodType(params)
        .then(res => {
          console.log(res);
          if (res.success) {
            this.$message({
              message: "新增成功",
              type: "success"
            });
          }
          // done可以传入一个对象来修改提交的某个字段
          done();
          // 保存按钮的loading状态
          this.formOptions.saveLoading = false;
          // 重新加载列表
          let params = {
            page: this.page.pageIndex,
            limit: this.page.pageSize
          };
          this.init(params);
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 删除种类
    deleteGood({ index, row }) {
      let params = {
        id: row.id
      };
      this.$confirm("此操作将删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteGoodType(params)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
              }
              // 重新加载列表
              let params = {
                page: this.page.pageIndex,
                limit: this.page.pageSize
              };
              this.init(params);
            })
            .catch(err => {
              this.$message.error(err);
            });
        })
        .catch(() => {
        });
    },

    // 条件查询搜索
    searchClick() {
      let params = {
        page: 1
      };
      params.name = this.FormSearch.name;
      this.init(params);
    },

    // 重置查询
    resetForm() {
      this.FormSearch.name = "";
      let params = {
        page: 1,
        limit: 10
      };
      this.init(params);
    },

    // 编辑提交
    handleRowEdit({ index, row }, done) {
      // 保存按钮的loading状态
      this.formOptions.saveLoading = true;
      let params = {};
      params.id = row.id
      params.name = row.name
      updateGoodType(params)
        .then(res => {
          console.log(res);
          if (res.success) {
            this.$message({
              message: "编辑成功",
              type: "success"
            });
          }
          // done可以传入一个对象来修改提交的某个字段
          done();
          // 保存按钮的loading状态
          this.formOptions.saveLoading = false;
          // 重新加载列表
          let params = {
            page: this.page.pageIndex,
            limit: this.page.pageSize
          };
          this.init(params);
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 取消编辑和添加
    handleDialogCancel(done) {
      /* this.$message({
        message: "取消操作",
        type: "warning"
      }); */
      done();
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
